package com.guodu.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;

import com.guodu.dao.SupplierQualifiedDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.SupplierQualified;
import com.guodu.entity.SupplierQualifiedS;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.SupplierQualifiedService;
import com.guodu.vo.SupplierQualifiedQueryVo;
import com.guodu.vo.SupplierQualifiedVo;

@Service("supplierQualifiedService")
public class SupplierQualifiedServiceImpl implements SupplierQualifiedService{
	
	@Resource
	private SupplierQualifiedDao supplierQualifiedDao;
	
	@Resource
	private UserInfoDao userInfoDao;
	
	@Resource
	private UnitsDao unitsDao;

	public void setSupplierQualifiedDao(SupplierQualifiedDao supplierQualifiedDao) {
		this.supplierQualifiedDao = supplierQualifiedDao;
	}

	
	public List<SupplierQualifiedVo> findAll(SupplierQualifiedQueryVo supplierQualifiedQueryVo,Units units,UserInfo userInfo,
			int pageSize, int curPage) {
		List<SupplierQualifiedVo> supplierQualifiedVoList  = new ArrayList<SupplierQualifiedVo>();
		List<SupplierQualified> supplierQualifiedList = supplierQualifiedDao.findAll(supplierQualifiedQueryVo,units,userInfo, pageSize, curPage);
		for(int i=0;i<supplierQualifiedList.size();i++){
			SupplierQualifiedVo supplierQualifiedVo2 = new SupplierQualifiedVo();
			UserInfo createUser = userInfoDao.findById(supplierQualifiedList.get(i).getCreatePeople());
			Units createUnits= unitsDao.findById(createUser.getUnitsId());
			supplierQualifiedVo2.setSupplierQualified(supplierQualifiedList.get(i));
			supplierQualifiedVo2.setUserName(createUser.getUserName());
			supplierQualifiedVo2.setUnitsId(createUser.getUnitsId());
			supplierQualifiedVo2.setUnitsLevel(createUnits.getUnitsLevel());
			supplierQualifiedVoList.add(supplierQualifiedVo2);
		}		
		return supplierQualifiedVoList;
	}
	public List<SupplierQualified> downLoad(
			SupplierQualifiedQueryVo supplierQualifiedQueryVo,Units units,UserInfo userInfo) {
		
		return supplierQualifiedDao.downLoad(supplierQualifiedQueryVo,units,userInfo);
	}

	public int findCount(SupplierQualifiedQueryVo supplierQualifiedQueryVo,Units units,UserInfo userInfo) {
		
		return supplierQualifiedDao.findCount(supplierQualifiedQueryVo,units,userInfo);
	}


	
	public void save(SupplierQualified supplierQualified) {
		
		supplierQualifiedDao.save(supplierQualified);
	}


	
	public void delByIds(String[] ids,String [] fjurls) {
		for(int i=0;i<fjurls.length;i++){
			String fjurl = fjurls[i]; 
			String oldPath = ServletActionContext.getServletContext()
			.getRealPath("/")+ "upload//" + fjurl;
			File oldfile = new File(oldPath);
			oldfile.delete();			
		}	
		supplierQualifiedDao.delByIds(ids);
	}

	public void examine(String ids, String reason, String beforeStatus,
			Long afterStatus, UserInfo userInfo) {
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<SupplierQualifiedS> SupplierQualifiedSList = new ArrayList<SupplierQualifiedS>();
		for(int i=0;i<idses.length;i++){
			SupplierQualifiedS supplierQualifiedS = new SupplierQualifiedS();
			supplierQualifiedS.setAfterStatus(afterStatus);
			supplierQualifiedS.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			supplierQualifiedS.setCreatePeople(userInfo.getUserId());
			supplierQualifiedS.setSupplierId(idses[i]);
			supplierQualifiedS.setReason(reason);
			supplierQualifiedS.setCreateTime(Calendar.getInstance().getTime());
			supplierQualifiedS.setStatusId(i+"");			
			SupplierQualifiedSList.add(supplierQualifiedS);
		}
		supplierQualifiedDao.changeStatus(SupplierQualifiedSList);
		supplierQualifiedDao.examine(ids, afterStatus);		
	}


	public SupplierQualified findById(String id) {
		
		return supplierQualifiedDao.findById(id);
	}


	
	public void update(SupplierQualified supplierQualified,File reportFile,String reportFileFileName,Long userId){
		if(reportFile != null){
			if(supplierQualified.getAnnexUrl() != null && supplierQualified.getAnnexUrl()!=""){
				String oldPath = ServletActionContext.getServletContext()
				.getRealPath("/")+ "upload//SupplierQualified//" + supplierQualified.getAnnexUrl();
				File oldfile = new File(oldPath);
				oldfile.delete();
			}
							
			String path = "";
			try {
				path = ServletActionContext.getServletContext().getRealPath("/")
				+ "upload//SupplierQualified//";
				this.createPath(path);
				String rename = userId + Calendar.getInstance().getTimeInMillis()+reportFileFileName; 				
				InputStream in = new FileInputStream(reportFile);
				OutputStream out = new FileOutputStream(path+rename);
				byte b[] = new byte[1024];
				int a = in.read(b);
				while (a != -1) {
					out.write(b);
					a = in.read(b);
				}				
				supplierQualified.setAnnexName(reportFileFileName);
				supplierQualified.setAnnexUrl(rename);				
				out.flush();
				out.close();
				in.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		
		if(supplierQualified.getStatus() == 4L){
			List<SupplierQualifiedS> SupplierQualifiedSList = new ArrayList<SupplierQualifiedS>(); 
			SupplierQualifiedS supplierQualifiedS = new SupplierQualifiedS();
			supplierQualifiedS.setAfterStatus(0L);
			supplierQualifiedS.setBeforeStatus(supplierQualified.getStatus());
			supplierQualifiedS.setCreatePeople(userId);
			supplierQualifiedS.setSupplierId(supplierQualified.getSupplierId());
			supplierQualifiedS.setReason("");
			supplierQualifiedS.setCreateTime(Calendar.getInstance().getTime());
			supplierQualifiedS.setStatusId("1");			
			SupplierQualifiedSList.add(supplierQualifiedS);
			supplierQualifiedDao.changeStatus(SupplierQualifiedSList);			
		}
		supplierQualified.setStatus(0L);
		supplierQualifiedDao.update(supplierQualified);
	}

	public List<SupplierQualifiedS> findStatus(String id) {
		
		return supplierQualifiedDao.findStatus(id);
	}

	public void deleteById(String id, String fileUrl) {
		if(fileUrl != null && fileUrl != ""){
			String oldPath = ServletActionContext.getServletContext()
			.getRealPath("/")+ "upload//SupplierQualified//" +fileUrl;
			File oldfile = new File(oldPath);
			oldfile.delete();	
		}				
		supplierQualifiedDao.deleteById(id);
		
	}
	
	private void createPath(String path) {
		File file = new File(path);
		if (file.exists() == false) {
			file.mkdir();
		}
	}


	
	public void examinAll(SupplierQualifiedQueryVo supplierQualifiedQueryVo,
			Units units, UserInfo userInfo) {
		
		supplierQualifiedDao.examinAll(supplierQualifiedQueryVo,units,userInfo);
	}

	
	
	

}
